package de.game.gridlogic;

import android.util.Log;
import de.game.Constants;

public class GridImpl implements Grid {

	private int height, width, ID;
	private String name;
	private int[] playerLocation = new int[2];
	private int[][] cellList;
	
	public GridImpl(){}
	
	private GridImpl(String name, int ID, int width, int height){
		this.name = name;
		this.ID = ID;
		this.width = width;
		this.height = height;
		
		cellList = new int[width][height];
	}

	@Override
	public int getCell(int x, int y) {
		return cellList[x][y];
	}
	
	@Override
	public int[] getRow(int y) {
		int[] row = new int[width];
			for(int x = 0; x < width; x++){
				row[x] = cellList[x][y];
			}
		return row; 
	}

	@Override
	public void setCell(int x, int y, int content) {
		cellList[x][y] = content;
		
	}

	@Override
	public void setRow(int y, int[] contents) {
		for(int x = 0; x < width; x++){
			cellList[x][y] = contents[x];
		}
	}

	@Override
	public String getName() {
		return name;
	}

	public int[][] getCellList() {
		return cellList;
	}	

	@Override
	public int getId() {
		return ID;
	}

	@Override
	public float getEuclideanDistance(int x1, int y1, int x2, int y2) {
		return (float) Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
	}

	@Override
	public float getManhattanDistance(int x1, int y1, int x2, int y2) {
		return (float) Math.abs(x1-x2) + Math.abs(y1-y2);
	}

	@Override
	public int[] getPlayerLocation() {
		return playerLocation;
	}
	
	@Override
	public String getLocation(){
		return playerLocation[0]+" "+playerLocation[1];
	}
	
	@Override
	public boolean placePlayer(int x, int y) {
			int[] location = new int[2];
			location[0]=x;
			location[1]=y;
			setPlayerLocation(location);
			return true;
	}
	
	public void setPlayerLocation(int[] playerLocation) {
		this.playerLocation = playerLocation;
	}

	@Override
	public boolean isLeftClear(int x, int y) {
		if(x == 0 || cellList[x-1][y] > 0){
			return false;
		}
		return true;
	}

	@Override
	public boolean isRightClear(int x, int y) {
		if(x == width-1 || cellList[x+1][y] > 0){
			return false;
		}
		return true;
	}

	@Override
	public boolean isUpClear(int x, int y) {
			if(y == 0 || cellList[x][y-1] > 0){
				return false;
			}
			return true;
	}

	@Override
	public boolean isDownClear(int x, int y) {
		if(y == height-1 || cellList[x][y+1] > 0){
			return false;
		}
		return true;
	}

	@Override
	public String toString(){
		return ("Grid Name und ID: "+this.getName()+ " und "+this.getId()+ ". Breite und Hoehe: "+ this.width+", "+ this.height+ ".");
		
	}
	

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public static Grid createGrid(String name, int ID, int width, int height) {
		return new GridImpl(name, ID, width, height);
	}
	
	public static GridImpl createRandomGrid(){
		int[][] rows = new int[24][20];
		GridImpl grid = (GridImpl) GridImpl.createGrid("testrdm", 3, 20, 24);	// name, id, width, height
		
		for(int j = 0; j < 24; j++){
			for(int i = 0; i < 20; i++){
				if(Math.random()*10 > 7){
					Log.v(Constants.LOG_ID,"grid input = 2");
					rows[j][i] = 2;			// wand
				} else {
					Log.v(Constants.LOG_ID,"grid input = 0");
					rows[j][i] = 0;			// freies feld
				}
			}
		}
		
		for(int i = 0; i < 24; i++){
			grid.setRow(i, rows[i]);
		}
		
		return grid;
	}
	
}
